<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Tag_Hooks_Tagger_Hook extends Gio_Core_Hook
{
	public static function show($itemName, $routeName, $detailsRouteName, $itemId = null)
	{
		$view = Gio_Core_View::getInstance();
		if ($itemId) {
			$item = array(
							'item_id' 			 => $itemId,
							'item_name' 		 => $itemName,
							'route_name' 		 => $routeName,
							'details_route_name' => $detailsRouteName,
			);
			$tags = Modules_Tag_Services_Tag::getByItem($item);
			$view->tags = $tags;
		}
		
		$view->tagItemName         = $itemName;
		$view->tagItemRouteName    = $routeName;
		$view->tagDetailsRouteName = $detailsRouteName;
		$viewFile = TEMPLATE_DIR . DS . $view->APP_TEMPLATE . DS . 'modules'
							 . DS . 'tag' . DS . '_partial' . DS . '_tagger.phtml';
		echo $view->render($viewFile, true);
	}
	
	public static function add($itemId)
	{
		$request       = Gio_Core_Request::getInstance();
		$itemName 	   = $request->getParam('tagItemName');
		$itemRouteName = $request->getParam('tagItemRouteName');
		$detailsRoute  = $request->getParam('tagDetailsRouteName');
		$tagIds 	   = $request->getParam('tagIds');
	
		if ($tagIds) {
			Modules_Tag_Services_TagItem::delete(array(
											'item_id' 			 => $itemId,
											'item_name' 		 => $itemName,
											'route_name' 		 => $itemRouteName,
											'details_route_name' => $detailsRoute,
										));
			
			foreach ($tagIds as $tagId) {
				Modules_Tag_Services_TagItem::add(array(
											'tag_id' 			 => $tagId,
											'item_id' 			 => $itemId,
											'item_name' 		 => $itemName,
											'route_name' 		 => $itemRouteName,
											'details_route_name' => $detailsRoute,
											'params' 			 => $itemName . ':' . $itemId,
										));
			}
		}
	}
}
